<?php
$page_title = 'eTAT: Survey Content';
$style = 'survey';
$script = 'survey';
require_once '../controller.php';
if(!isset($params->sid)) {
	redirect('survey/');
}
$s = new Survey($params->sid);
if(isset($_POST['submit_question']) || isset($_POST['submit_section'])) {
	if(isset($_POST['required'])){
		$required = 'Y';
	} else {
		$required = 'N';
	}
	if(isset($_POST['submit_section'])) {
		$section = 'Y';
		$_POST['type'] = 'section';
	} else {
		$section = 'N';
	}
	$data = (object) array(
		'survey_id'=>$s->id,
		'type'=>$_POST['type'],
		'required'=>$required,
		'question'=>addslashes($_POST['question_text']),
		'create_section'=>$section,
		'order'=>addslashes($_POST['order']),
		'response1'=>addslashes($_POST['answer_choice_1']),
		'response2'=>addslashes($_POST['answer_choice_2']),
		'response3'=>addslashes($_POST['answer_choice_3']),
		'response4'=>addslashes($_POST['answer_choice_4']),
		'response5'=>addslashes($_POST['answer_choice_5']),
		'response6'=>addslashes($_POST['answer_choice_6'])
	);
	if(isset($params->qid)) {
		$q = $s->getQuestion($params->qid);
		$q->updateQuestion($data);
		redirect("survey/content.php?sid=$s->id");
	} else {
		$s->addQuestion($data);
		redirect("survey/content.php?sid=$s->id");
	}
}
if(isset($_POST['confirm_delete'])) {
	$s->deleteQuestion($_POST['qid']);
	redirect("survey/content.php?sid=$s->id");
}
require_once '../header.php';
print '<h1>'.anchor('survey/','Surveys',array('noselect'=>false)).' > '.$s->survey_name.' Content</h1>';
?>
<p>Add/Modify content in <b><?php print $s->survey_name; ?></b>:</p>
<br />
<?php if(isset($params->qid)) { ?>
<div id='edit-question'>
<?php
	$q = $s->getQuestion($params->qid);
	if($q->create_section == 'Y') {
		$form = new Form('edit_section_form','post');
		$form->text('<h3>Edit Section</h3>');
		$form->textArea('question_text','Section Text',1,30,array('value'=>$q->question,'required'=>true));
		$form->button('submit_section','Submit');
		print $form->build();
	} else {
		function select($name,$check) {
			if($name == $check) {
				return true;
			} else {
				return false;
			}
		}
		$form = new Form('edit_question_form','post');
		$form->text('<h3>Edit Question:</h3>');
		$form->selectStart('type','Question Type');
		$form->selectOption('text','Text',array('onclick'=>'hideAnswers()','selected'=>select('text',$q->type)));
		$form->selectOption('yesno','Yes/No',array('onclick'=>'hideAnswers()','selected'=>select('yesno',$q->type)));
		$form->selectOption('numeric','Numeric',array('onclick'=>'hideAnswers()','selected'=>select('numeric',$q->type)));
		$form->selectOption('date','Date',array('onclick'=>'hideAnswers()','selected'=>select('date',$q->type)));
		$form->selectOption('radio','Radio',array('onclick'=>'showAnswers()','selected'=>select('radio',$q->type)));
		$form->selectOption('checkbox','Checkbox',array('onclick'=>'showAnswers()','selected'=>select('checkbox',$q->type)));
		$form->selectOption('rate','Rate',array('onclick'=>'showAnswers()','selected'=>select('rate',$q->type)));
		$form->selectOption('dropdown','Dropdown',array('onclick'=>'showAnswers()','selected'=>select('dropdown',$q->type)));
		$form->selectEnd();
		$form->divStart('question-content');
		$form->textArea('question_text','Question Text',1,30,array('value'=>$q->question,'required'=>true));
		$form->divStart('answer-choices',array('class'=>'invisible'));
		$form->text('<p>Add Possible Answer Choices:</p>');
		$form->textInput('answer_choice_1','Answer 1',array('value'=>$q->response1));
		$form->textInput('answer_choice_2','Answer 2',array('value'=>$q->response2));
		$form->textInput('answer_choice_3','Answer 3',array('value'=>$q->response3));
		$form->textInput('answer_choice_4','Answer 4',array('value'=>$q->response4));
		$form->textInput('answer_choice_5','Answer 5',array('value'=>$q->response5));
		$form->textInput('answer_choice_6','Answer 6',array('value'=>$q->response6));
		$form->divEnd();
		$form->divEnd();
		$form->button('submit_question','Submit',array(),false);
		$form->button('cancel','Cancel',array('type'=>'button','onclick'=>"redirect('survey/content.php?sid=$s->id')"),false);
		print $form->build();
	}
?>
</div>
<?php
} else {
	$questions = $s->getAllQuestions();
?>
<div id='questions'>
	<ul>
	<?php
		if($questions) {
			foreach($questions as $q) {
				if($q->create_section == 'N') {
					print "<li id='$q->id'>$q->question ($q->type) ";
				} else {
					print "<li id='$q->id'><---- Section: $q->question -----> ";
				}
					print anchor("survey/content.php?sid=$s->id&qid=$q->id",'edit',array()).' '.
						anchor("#delete-$q->id",'delete',array('onclick'=>'popup("confirm-delete")')).
						'</li>';
			}
			$form = new Form('delete_question','post',true,array('class'=>'center'));
			$form->text('<p>Confirm delete:</p>');
			$form->textInput('qid','Question ID',array('class'=>'invisible','value'=>$q->id));
			$form->button('confirm_delete','Yes',array(),false);
			$form->button('cancel_delete','No',array('type'=>'button','onclick'=>'javascript:closePopup();'),false);
			print popup('confirm-delete',$form->build(),120,array('left'=>300));
		}
	?>
	<li id='selection'>
	<?php
		print newBtn('add_question','Add Question',array('type'=>'button','onclick'=>"$('add-section-form').className = 'invisible'; $('add-question-form').toggleClass('invisible')"),false);
		print newBtn('add_section','Add Section',array('type'=>'button','onclick'=>"$('add-question-form').className = 'invisible'; $('add-section-form').toggleClass('invisible')"),false);
		$form1 = new Form('add_question_form','post',true,array('class'=>'invisible'));
		$form1->text('<h3>Add Question</h3>');
		$form1->selectStart('type','Question Type');
		$form1->selectOption('text','Text',array('onclick'=>'hideAnswers()'));
		$form1->selectOption('yesno','Yes/No',array('onclick'=>'hideAnswers()'));
		$form1->selectOption('numeric','Numeric',array('onclick'=>'hideAnswers()'));
		$form1->selectOption('date','Date',array('onclick'=>'hideAnswers()'));
		$form1->selectOption('radio','Radio',array('onclick'=>'showAnswers()'));
		$form1->selectOption('checkbox','Checkbox',array('onclick'=>'showAnswers()'));
		$form1->selectOption('rate','Rate',array('onclick'=>'showAnswers()'));
		$form1->selectOption('dropdown','Dropdown',array('onclick'=>'showAnswers()'));
		$form1->selectEnd();
		$form1->divStart('question-content');
		$form1->textArea('question_text','Question Text',1,30,array('value'=>'','required'=>true));
		$form1->divStart('answer-choices',array('class'=>'invisible'));
		$form1->text('<p>Add Possible Answer Choices:</p>');
		$form1->textInput('answer_choice_1','Answer 1');
		$form1->textInput('answer_choice_2','Answer 2');
		$form1->textInput('answer_choice_3','Answer 3');
		$form1->textInput('answer_choice_4','Answer 4');
		$form1->textInput('answer_choice_5','Answer 5');
		$form1->textInput('answer_choice_6','Answer 6');
		$form1->divEnd();
		$form1->divEnd();
		$form1->button('submit_question','Submit',array(),false);
		$form1->button('cancel','Cancel',array('type'=>'button','onclick'=>"$('add-question-form').className = 'invisible'"),false);
		print $form1->build();
		$form2 = new Form('add_section_form','post',true,array('class'=>'invisible'));
		$form2->text('<h3>Add Section</h3>');
		$form2->textArea('question_text','Section Text',1,30,array('value'=>'','required'=>true));
		$form2->button('submit_section','Submit',array(),false);
		$form2->button('cancel','Cancel',array('type'=>'button','onclick'=>"$('add-section-form').className = 'invisible'"),false);
		print $form2->build();
	?>
	</li>
	</ul>
</div>
<?php
}
	print '<br/>';
	if(!isset($params->qid)) {
		print newBtn('finish','Preview Survey',array('type'=>'button','onclick'=>"redirect('survey/preview.php?sid=$s->id&sec=1&num=0');"));
	}
	print newBtn('finish','Save & Close',array('type'=>'button','onclick'=>'redirect("survey/");'));
?>
<div class='clear'></div>
<?php require_once '../footer.php'; ?>